package com.xh.rufongwallpaper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xh.rufongwallpaper.entity.Device;
import com.xh.rufongwallpaper.entity.Favorite;
import com.xh.rufongwallpaper.entity.Wallpaper;
import com.xh.rufongwallpaper.mapper.FavoriteMapper;
import com.xh.rufongwallpaper.mapper.WallpaperMapper;
import com.xh.rufongwallpaper.service.DeviceService;
import com.xh.rufongwallpaper.service.FavoriteService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * 收藏服务实现类
 *
 * @author XH
 */
@Service
@RequiredArgsConstructor
public class FavoriteServiceImpl implements FavoriteService {

    private final FavoriteMapper favoriteMapper;
    private final WallpaperMapper wallpaperMapper;
    private final DeviceService deviceService;

    @Override
    @Transactional
    public boolean addFavorite(String deviceId, Long wallpaperId) {
        // 获取设备ID
        Optional<Device> deviceOpt = deviceService.getDeviceByDeviceId(deviceId);
        if (deviceOpt.isEmpty()) {
            return false;
        }
        
        Device device = deviceOpt.get();
        
        // 检查壁纸是否存在
        Wallpaper wallpaper = wallpaperMapper.selectById(wallpaperId);
        if (wallpaper == null || wallpaper.getIsDeleted() || wallpaper.getStatus() != 1) {
            return false;
        }
        
        // 检查是否已收藏
        boolean exists = favoriteMapper.exists(
                new LambdaQueryWrapper<Favorite>()
                        .eq(Favorite::getDeviceId, device.getId())
                        .eq(Favorite::getWallpaperId, wallpaperId)
        );
        
        if (exists) {
            return true; // 已收藏，视为操作成功
        }

        // 创建新收藏
        Favorite favorite = new Favorite();
        favorite.setDeviceId(device.getId());
        favorite.setWallpaperId(wallpaperId);

        return favoriteMapper.insert(favorite) > 0;
    }

    @Override
    @Transactional
    public boolean removeFavorite(String deviceId, Long wallpaperId) {
        // 获取设备ID
        Optional<Device> deviceOpt = deviceService.getDeviceByDeviceId(deviceId);
        if (deviceOpt.isEmpty()) {
            return false;
        }
        
        Device device = deviceOpt.get();
        
        return favoriteMapper.delete(
                new LambdaQueryWrapper<Favorite>()
                        .eq(Favorite::getDeviceId, device.getId())
                        .eq(Favorite::getWallpaperId, wallpaperId)
        ) > 0;
    }

    @Override
    public Page<Wallpaper> getFavoriteWallpapers(String deviceId, int pageIndex, int pageSize) {
        // 获取设备ID
        Optional<Device> deviceOpt = deviceService.getDeviceByDeviceId(deviceId);
        if (deviceOpt.isEmpty()) {
            return new Page<>(pageIndex + 1, pageSize);
        }
        
        Device device = deviceOpt.get();
        
        // 获取收藏的壁纸ID列表
        List<Favorite> favorites = favoriteMapper.selectList(
                new LambdaQueryWrapper<Favorite>()
                        .eq(Favorite::getDeviceId, device.getId())
        );
        
        if (favorites.isEmpty()) {
            return new Page<>(pageIndex + 1, pageSize);
        }
        
        // 提取壁纸ID列表
        List<Long> wallpaperIds = new ArrayList<>();
        for (Favorite favorite : favorites) {
            wallpaperIds.add(favorite.getWallpaperId());
        }
        
        // 分页查询收藏的壁纸
        Page<Wallpaper> page = new Page<>(pageIndex + 1, pageSize);
        return wallpaperMapper.selectPage(page,
                new LambdaQueryWrapper<Wallpaper>()
                        .in(Wallpaper::getId, wallpaperIds)
                        .eq(Wallpaper::getIsDeleted, false)
                        .eq(Wallpaper::getStatus, 1)
                        .orderByDesc(Wallpaper::getCreateTime)
        );
    }

    @Override
    public boolean isFavorite(String deviceId, Long wallpaperId) {
        // 获取设备ID
        Optional<Device> deviceOpt = deviceService.getDeviceByDeviceId(deviceId);
        if (deviceOpt.isEmpty()) {
            return false;
        }
        
        Device device = deviceOpt.get();
        
        return favoriteMapper.exists(
                new LambdaQueryWrapper<Favorite>()
                        .eq(Favorite::getDeviceId, device.getId())
                        .eq(Favorite::getWallpaperId, wallpaperId)
        );
    }

    @Override
    public long countFavoritesByDeviceId(String deviceId) {
        // 获取设备ID
        Optional<Device> deviceOpt = deviceService.getDeviceByDeviceId(deviceId);
        if (deviceOpt.isEmpty()) {
            return 0;
        }
        
        Device device = deviceOpt.get();
        
        return favoriteMapper.selectCount(
                new LambdaQueryWrapper<Favorite>()
                        .eq(Favorite::getDeviceId, device.getId())
        );
    }
} 